﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AddJob.aspx.cs" Inherits="Quartz.Web.AddJob" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link href="script/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
    <link href="css/common.css" rel="stylesheet" type="text/css" />
    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="script/ligerUI/js/core/base.js" type="text/javascript"></script>
    <script src="script/ligerUI/js/plugins/ligerTab.js" type="text/javascript"></script>
    <script src="script/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"></script>
    <script src="script/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script>
    <script src="script/ligerUI/js/plugins/ligerTip.js" type="text/javascript"></script>
    <script src="script/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
    <script src="../Scripts/Public.js" type="text/javascript"></script>
    
    <script type="text/javascript">
        $(document).ready(function () {
            var type = getPar1("type");
            $("#tab").ligerTab();
            //快速设置表单默认的按钮:保存、取消
            var buttons = [];
            buttons.push({ text: '关闭', onclick: function () {
                //关闭当前页面
                parent.parent.CloseCurPage();
            }
            });
            if (type == "add" || type == "edit") {
                //加载任务组
                var url = "quartz.aspx";
                var param = [
                { name: "flag", value: "GetJobGroupNameLists" },
				{ name: "r", value: Math.random()}];
                $.ajax({
                    type: 'post',
                    url: url,
                    data: param,
                    dataType: "json",
                    success: function (data) {
                        if (data == null || data.length == 0) return;
                        for (var p in data) {
                            $("#selectJobGroup").append("<option title='" + data[p] + "'>" + data[p] + "</option>");
                        }
                        $("#JobGroup").val(data[0]);
                    },
                    error: function (data) {
                    }
                });
                buttons.push({ text: '保存', onclick: function () {
                    var jobGroupName = $("#JobGroup").val();
                    var jobName = $("#JobName").val();
                    var jobType = $("#JobType").val();
                    var jobDes = $("#JobDes").val();
                    //数据验证
                    if (jobName == "") {
                        alert("任务名称不能为空！");
                        return;
                    }
                    if (jobGroupName == "") {
                        alert("任务组名不能为空！");
                        return;
                    }
                    if (jobType == "") {
                        alert("任务类型不能为空！");
                        return;
                    }
                    if (!CheckUploadFile()) return;
                    ajaxFileUpload();
                }
                });
            }
            if (!buttons) return;
            var formbar = $("body > div.form-bar");
            if (formbar.length == 0)
                formbar = $('<div class="form-bar"><div class="form-bar-inner"></div></div>').appendTo('body');
            if (!(buttons instanceof Array)) {
                buttons = [buttons];
            }
            $(buttons).each(function (i, o) {
                var btn = $('<div class="l-dialog-btn"><div class="l-dialog-btn-l"></div><div class="l-dialog-btn-r"></div><div class="l-dialog-btn-inner"></div></div> ');
                $("div.l-dialog-btn-inner:first", btn).html(o.text || "BUTTON");
                if (o.onclick) {
                    btn.bind('click', function () {
                        o.onclick(o);
                    });
                }
                if (o.width) {
                    btn.width(o.width);
                }
                $("> div:first", formbar).append(btn);
            });
            var columnsData = null;
            var toolbar = null;
            if (type == "add") {
                columnsData = [
                { display: '计划名称', name: 'planName', align: 'left', width: 150, minWidth: 100 },
                { display: '计划组', name: 'planGroupName', align: 'left', width: 150, minWidth: 100 },
                { display: '计划类型', name: 'planType', width: 100, minWidth: 100 },
                { display: '执行状态', name: 'planStatus', width: 100, minWidth: 100 },
                { display: '开始时间', name: 'planStartTime', width: 120, minWidth: 100 },
                { display: '结束时间', name: 'planEndTime', width: 120, minWidth: 100 },
                { display: '计划说明', name: 'planDes', align: 'left', width: 350, minWidth: 100,
                    render: function (item) {
                        var planDes = item.planDes;
                        if (planDes.length > 40) planDes = planDes.substr(0, 40) + "...";
                        return "<div title='" + item.planDes + "'>" + planDes + "</div>";
                    }
                },
                { display: '表达式', name: 'planCron', width: 0, isAllowHide: true, hide: true, editor: { type: 'hidden'} }
                ];
                toolbar = { items: [
                { text: '增加', click: fnAddTaskPlan, img: 'script/ligerUI/skins/icons/add.gif' },
                { line: true },
                { text: '修改', click: fnEditTaskPlan, img: 'script/ligerUI/skins/icons/edit.gif' },
                { line: true },
                { text: '删除', click: fnDeleteTaskPlan, img: 'script/ligerUI/skins/icons/delete.gif' }
                ]
                };
            }
            else if (type == "edit") {
            }
            else if (type == "view") {
                columnsData = [
                { display: '计划名称', name: 'planName', align: 'left', width: 100, minWidth: 60 },
                { display: '计划组', name: 'planGroupName', align: 'left', width: 100, minWidth: 60 },
                { display: '优先级', name: 'planPriority', width: 80, minWidth: 50 },
                { display: '开始时间', name: 'planStartTime', align: 'left', width: 150, minWidth: 100 },
                { display: '结束时间', name: 'planEndTime', align: 'left', width: 150, minWidth: 100 },
                { display: '上一次执行时间', name: 'planPreFireTime', align: 'left', width: 150, minWidth: 100 },
                { display: '下一次执行时间', name: 'planNextFireTime', align: 'left', width: 150, minWidth: 100 },
                { display: '计划说明', name: 'planDes', align: 'left', align: 'left', width: 350, minWidth: 100,
                    render: function (item) {
                        var planDes = item.planDes;
                        if (planDes.length > 40) planDes = planDes.substr(0, 40) + "...";
                        return "<div title='" + item.planDes + "'>" + planDes + "</div>";
                    }
                }
                ];
                toolbar = null;
            }
            //加载执行计划
            $("#taskPlanGrid").ligerGrid({
                width: '98%',
                height: '88%',
                columns: columnsData,
                pageSize: 20,
                rownumbers: true,
                toolbar: toolbar
            });
            if (type == "view" || type == "edit") { //查看或编辑时
                var jobName = getPar1("jobName");
                var jobGroupName = getPar1("jobGroupName");
                var jobType = getPar1("jobType");
                var jobStatus = getPar1("jobStatus");
                var jobDes = getPar1("jobDes");
                $("#JobName").val(jobName);
                $("#JobGroup").val(jobGroupName);
                $("#JobType").val(jobType);
                $("#JobDes").val(jobDes);
                if (type == "view") {
                    $("#jobTbody").append("<tr><td class='td_text'>任务状态：</td><td class='td_value' colspan='3'><input id='JobStatus' readonly='readonly' type='text' /></td></tr>");
                    $("#jobTbody input").attr("readonly", true); //.css("background", "whitesmoke")
                    $("#jobTbody select").attr("disabled", true);
                    $("#JobStatus").val(jobStatus);
                }
                //获取任务计划详细数据
                var url = "quartz.aspx";
                var param = [
                    { name: "flag", value: "GetJobPlanInfo" },
                    { name: "jobName", value: jobName },
                    { name: "jobGroupName", value: jobGroupName },
                    { name: "op", value: type },
                    { name: "r", value: Math.random()}];
                $.ajax({
                    type: 'post',
                    url: url,
                    data: param,
                    dataType: "json",
                    success: function (data) {
                        var grid = $("#taskPlanGrid").ligerGetGridManager();
                        for (var i = 0; i < data.Rows.length; i++) {
                            grid.addRow({
                                planName: data.Rows[i].planName,
                                planGroupName: data.Rows[i].planGroupName,
                                planPriority: data.Rows[i].planPriority,
                                planStartTime: data.Rows[i].planStartTime,
                                planEndTime: data.Rows[i].planEndTime,
                                planPreFireTime: data.Rows[i].planPreFireTime,
                                planNextFireTime: data.Rows[i].planNextFireTime,
                                planDes: data.Rows[i].planDes
                            });
                        }
                    },
                    error: function (data) {
                    }
                });
            }
            else {  //新增时
                $("#jobTbody").append("<tr><td class='td_text'>任务类库：</td><td class='td_value' colspan='3'><form id='form' enctype='multipart/form-data' method='post' action='quartz.aspx?flag=UploadJobDll' target='iframe_upfile'><iframe id='iframe_upfile' name='iframe_upfile' style='display:none'></iframe><input id='jobDll' name='jobDll' type='file' style='width:485px' /></form></td></tr>");
            }
        });

        var fnAddTaskPlan = function () {
            //弹出对话框
            $.ligerDialog.open({
                title: "新增任务执行计划",
                url: "AddJobPlan.aspx",
                width: 600, height: 450, isResize: true,
                name: "editWindow",
                buttons:
                [
                    { text: '保存', onclick: function (item, dialog) {
                        var editwindow = document.getElementById("editWindow");
                        var planName = editwindow.contentWindow.document.getElementById('PlanName').value;
                        var planGroupName = editwindow.contentWindow.document.getElementById('PlanGroup').value;
                        var planType = editwindow.contentWindow.document.getElementById('IsRepeat').value == "0" ? "重复执行" : "执行一次";
                        var planStatus = editwindow.contentWindow.document.getElementById('PlanStatus').value == "0" ? "等待执行" : "立即执行";
                        var planStartTime = editwindow.contentWindow.document.getElementById('PlanStartTime').value;
                        var planEndTime = editwindow.contentWindow.document.getElementById('PlanEndTime').value;
                        var planDes = editwindow.contentWindow.document.getElementById('PlanDes').value;
                        var planCron = editwindow.contentWindow.document.getElementById('CronExp').value;
                        var grid = $("#taskPlanGrid").ligerGetGridManager();
                        grid.addRow({
                            planName: planName,
                            planGroupName: planGroupName,
                            planType: planType,
                            planStatus: planStatus,
                            planStartTime: planStartTime,
                            planEndTime: planEndTime,
                            planDes: planDes,
                            planCron: planCron
                        });
                        dialog.close();
                    }
                    },
                    { text: '关闭', onclick: function (item, dialog) { dialog.close(); } }
                ]
            });
        }

        var fnEditTaskPlan = function ()
        { }

        var fnDeleteTaskPlan = function ()
        { }

        //保存任务数据
        function SaveJobData() {
            //获取新增的数据
            var saveData = [];
            var grid = $("#taskPlanGrid").ligerGetGridManager();
            var adddata = grid.getAdded();
            for (var i = 0; i < adddata.length; i++) {
                adddata[i].__status = "add";
                saveData.push(adddata[i]);
            }
            var jobInfo = "";
            var jobGroupName = $("#JobGroup").val();
            var jobName = $("#JobName").val();
            var jobType = $("#JobType").val();
            var jobDes = $("#JobDes").val();
            //任务数据序列化
            jobInfo = "{\"JobName\":\"" + jobName + "\",\"JobGroupName\":\"" + jobGroupName + "\",\"JobType\":\"" + jobType + "\",\"JobDes\":\"" + jobDes + "\",";
            var triggerName = "\"TriggerName\":[";
            var triggerGroupName = "\"TriggerGroupName\":[";
            var triggerCron = "\"TriggerCron\":[";
            var triggerDes = "\"TriggerDes\":[";
            for (var i = 0; i < saveData.length; i++) {
                triggerName += "\"" + saveData[i].planName + "\",";
                triggerGroupName += "\"" + saveData[i].planGroupName + "\",";
                triggerCron += "\"" + saveData[i].planCron + "\",";
                triggerDes += "\"" + saveData[i].planDes + "\",";
            }
            triggerName = triggerName.substr(0, triggerName.length - 1);
            triggerName += "]";
            triggerGroupName = triggerGroupName.substr(0, triggerGroupName.length - 1);
            triggerGroupName += "]";
            triggerCron = triggerCron.substr(0, triggerCron.length - 1);
            triggerCron += "]";
            triggerDes = triggerDes.substr(0, triggerDes.length - 1);
            triggerDes += "]";
            jobInfo += triggerName + "," + triggerGroupName + "," + triggerCron + "," + triggerDes;
            jobInfo += "}";
            var url = "quartz.aspx";
            var param = [
                    { name: "flag", value: "AddJob" },
                    { name: "jobInfo", value: jobInfo },
				    { name: "r", value: Math.random()}];
				    $.ajax({
				        type: 'post',
				        url: url,
				        data: param,
				        dataType: "json",
				        success: function (data) {
				            if (!data.IsError) {
				                alert("保存成功！");
				                //关闭当前页面
				                tabid = parent.$("#framecenter > .l-tab-content > .l-tab-content-item:visible").attr("tabid");
				                if (parent.tab) {
				                    parent.tab.removeTabItem(tabid);
				                }
				            }
				        },
				        error: function (data) {
				        }
				    });
        }
        
        //上传文件
        function ajaxFileUpload() {
            $("#form").submit();
        }
        //上传文件回调函数
        function fileUploadCallBack(status, msg) {
            if (status == 0) {
                SaveJobData();//DLL上传成功后保存任务
            }
            else {
                alert(msg);
            }
        }
        //上传文件验证
        var CheckUploadFile = function () {
            var filepath = $("#jobDll").val();
            if (filepath == "") {
                alert("上传文件不能为空!");
                return false;
            }
            var extStart = filepath.lastIndexOf(".");
            var ext = filepath.substring(extStart, filepath.length).toUpperCase();
            if (ext != ".DLL") {
                alert("上传文件仅限于dll格式!");
                return false;
            }
            return true;
        }
    </script>
    <style type="text/css">
      body
      {
          margin:0px 0px 0px 0px;
          font-size:13px;
      }
      td
      {
          height:30px;
      }
      .td_text
      {
          text-align:left;
          width:100px;
      }
      .td_value
      {
          text-align:left;
          width:200px;
      }
      input
      {
          width:180px;
      }
      select
      {
          width:184px;
      }
    </style>
</head>
<body>
    <div align="left" style="width:98%; margin:10px 20px 10px 20px;">
    <div style="height:20%;">
    <table cellpadding="0" cellspacing="0">
		<tbody id="jobTbody">
			<tr>
				<td class="td_text">
					任务组：</td>
				<td class="td_value">
                    <div style="position:relative;"> 
                    <span style="margin-left:174px;width:18px;overflow:hidden;"> 
                    <select id="selectJobGroup" style="margin-left:-174px;" onchange="this.parentNode.nextSibling.value=this.options[this.selectedIndex].text;"></select>
                    </span>
                    <input id="JobGroup" type="text" style="width:163px;height:19px;position:absolute;left:0px;" /> 
                    </div>
                </td>
			    <td class="td_text">
					任务名称：</td>
				<td class="td_value">
					<input id="JobName" name="JobName" title="任务名称" type="text" />
                </td>
            </tr>
            <tr>
                <td class="td_text">
					任务类型：</td>
				<td class="td_value" colspan="3">
					<input style="width:480px" id="JobType" name="JobType" title="任务类型" type="text" />
                </td>
            </tr>
			<tr>
			    <td class="td_text">
					任务说明：</td>
				<td class="td_value" colspan="3">
					<input style="width:480px" id="JobDes" name="JobDes" title="任务说明" type="text" />
                </td>
            </tr>
		</tbody>
	</table>
    </div>
    <div id="tab" style="margin:5px 0px 5px 0px; width: 98%;height:450px;overflow:hidden; border:1px solid #A3C0E8; ">
        <div tabid="tabTaskPlan" title="任务执行计划" style="height:100%;">
          <div id="taskPlanGrid" style="margin:10px";></div>
        </div>
    </div>
    </div>
</body>
</html>
